package cn.com.chengmandian.bike.rabbitmq;

import cn.com.chengmandian.bike.model.EbDeviceBike;
import cn.com.chengmandian.bike.model.EbRideCommand;
import cn.com.chengmandian.bike.model.EbRideOrder;
import cn.com.chengmandian.bike.service.IEbDeviceBikeService;
import cn.com.chengmandian.bike.service.IEbRideCommandService;
import cn.com.chengmandian.bike.service.IEbRideOrderService;
import cn.com.chengmandian.core.redis.cache.AgentCacheService;
import cn.com.chengmandian.core.web.pojo.AgentInfo;
import cn.com.chengmandian.core.web.utils.LocalMapUtil;
import cn.com.chengmandian.rabbitmq.constants.RabbitMqConstants;
import cn.com.chengmandian.service.bike.constants.DeviceCommandEnum;
import cn.com.chengmandian.service.bike.constants.RideCommandStatusEnum;
import cn.com.chengmandian.service.bike.constants.RideCommandTypeEnum;
import cn.com.chengmandian.service.bike.constants.UserTypeEnum;
import cn.com.chengmandian.service.bike.pojo.BikeCommandNotify;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component
public class OrderCommandNotifyReceiver {

    @Autowired
    private IEbRideOrderService rideOrderService;

    @Autowired
    private IEbRideCommandService rideCommandService;

    @Autowired
    private IEbDeviceBikeService deviceBikeService;

    @Autowired
    private AgentCacheService agentCacheService;

    private void handleRideCommand(BikeCommandNotify bikeCommandNotify) {
        if (bikeCommandNotify == null) return;
        log.info("handleRideCommand：{}", JSONObject.toJSONString(bikeCommandNotify));
        AgentInfo agentInfo = agentCacheService.getAgent(bikeCommandNotify.getAgentId() + "");
        LocalMapUtil.setAgentInfo(agentInfo);
        EbRideOrder rideOrder = rideOrderService.getByBike(bikeCommandNotify.getBikeId());
        if (rideOrder == null) return;
        EbRideCommand initCommand = rideCommandService.getCommand(rideOrder.getId(), bikeCommandNotify.getCommand(), RideCommandStatusEnum.INIT, RideCommandTypeEnum.RIDE);

        if (initCommand == null) {
            log.error("handleRideCommand：找不到指令 {}", rideOrder.getId());
            return;
        }

        if (DateUtil.between(initCommand.getCreateTime(), new Date(), DateUnit.SECOND) > 3000) {
            rideCommandService.failCommand(initCommand.getId(), "指令初始化超时");
            return;
        }
        try {
            // 增加命令执行轨迹
            EbDeviceBike deviceBike = deviceBikeService.getById(rideOrder.getBikeId());

            if (bikeCommandNotify.getCommand().equals(DeviceCommandEnum.OPEN_LOCK.getType())) {
                rideCommandService.orderOpenCommand(deviceBike, rideOrder, initCommand, "有响应开启订单");
            } else if (bikeCommandNotify.getCommand().equals(DeviceCommandEnum.STOP_LOCK.getType())) {
                rideCommandService.orderStopCommand(deviceBike, rideOrder, initCommand, UserTypeEnum.MEMBER.getCode());
            } else if (bikeCommandNotify.getCommand().equals(DeviceCommandEnum.CLOSE_LOCK.getType())) {
                rideCommandService.orderCloseCommand(deviceBike, rideOrder, initCommand, "有响应关闭订单", UserTypeEnum.MEMBER.getCode());
            }
            log.info("handleRideCommand 指令处理成功：{}", bikeCommandNotify.getBikeId());
        } catch (Exception e) {
            log.error("handleRideCommand操作失败：", e);
            rideCommandService.failCommand(initCommand.getId(), e.getMessage());
        }
    }

    @RabbitListener(queues = RabbitMqConstants.QUEUE_RIDE_COMMAND_OPEN_LOCK, ackMode = "AUTO")
    public void openLock(BikeCommandNotify bikeCommandNotify) {
        this.handleRideCommand(bikeCommandNotify);
    }

    @RabbitListener(queues = RabbitMqConstants.QUEUE_RIDE_COMMAND_STOP_LOCK, ackMode = "AUTO")
    public void stopLock(BikeCommandNotify bikeCommandNotify) {
        this.handleRideCommand(bikeCommandNotify);
    }

    @RabbitListener(queues = RabbitMqConstants.QUEUE_RIDE_COMMAND_CLOSE_LOCK, ackMode = "AUTO")
    public void closeLock(BikeCommandNotify bikeCommandNotify) {
        this.handleRideCommand(bikeCommandNotify);
    }
}
